<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* URI Language Identifier
*
* Adds a language identifier prefix to all site_url links
*
* version 0.15 (c) Wiredesignz 2009-02-25
*/
class MY_Language extends CI_Language
{
    function MY_Language()
    {
        parent::CI_Language();
        
        global $RTR;
        
        $index_page    = $RTR->config->item('index_page');
        $lang_uri_abbr = $RTR->config->item('lang_uri_abbr');
        $lang_subdomain_detection = $RTR->config->item('lang_subdomain_detection');        
        
        if($lang_subdomain_detection){
	        $host = $_SERVER['HTTP_HOST'];
	
	        if( substr($host,0,4) == 'www.') {
	        	$host = substr($host,4);
	        }
			
	        list ($lang_abbr) = explode('.', $host);
			
        } else{
        	 /* get the language from uri segment */
        	$lang_abbr = $RTR->uri->segment(1);
        }
        
        $base_url  = $RTR->config->item('base_url');
        $deft_abbr = $RTR->config->item('language_abbr');
            
        $user_lang;
        $user_lang_abbr;
       
        /* check for invalid abbreviation */
        if( ! isset($lang_uri_abbr[$lang_abbr]))
        {            
            /* check for abbreviation to be ignored */
            if ($deft_abbr != $RTR->config->item('lang_ignore'))
            {
                /* check and set the default uri identifier */
                $index_page .= ($index_page == '') ? "{$deft_abbr}" : "/{$deft_abbr}";
            
                $uri_string = $RTR->uri->uri_string;
            
                /* remove an invalid abbreviation from uri */
                if (strlen($lang_abbr) == 2)
                {
                    $uri_string = str_replace("/{$lang_abbr}", '', $uri_string);
                }
                
                /* redirect after inserting language id */
                header("Location:".$base_url.$index_page.$uri_string);
                //echo "Redirect: '"+"Location:".$base_url.$index_page.$uri_string+"'";
            }
            
            /* get the language name */
            $user_lang_abbr = $deft_abbr;
            $user_lang = $lang_uri_abbr[$deft_abbr];
        }
        else
        {
            /* get the language name */
            $user_lang = $lang_uri_abbr[$lang_abbr];
            $user_lang_abbr = $lang_abbr;
            
            /* reset config language to match the user language */
            $RTR->config->set_item('language', $user_lang);
            $RTR->config->set_item('language_abbr', $lang_abbr);
        
            /* check for abbreviation to be ignored */
            if ($lang_abbr != $RTR->config->item('lang_ignore'))
            {
                /* check and set the user uri identifier */
                $index_page .= ($index_page == '') ? "{$lang_abbr}" : "/{$lang_abbr}";
            }
        }
        
        /* reset the the config index_page value */
        $index_page .= ($index_page == '') ? '' : '/'; 
        
        if(!$lang_subdomain_detection){
        	$RTR->config->set_item('index_page', $index_page);
        } else{
        	//$RTR->config->set_item('base_url', "Pruebas");
        	$new_base_url = "";
        	$base_url = $RTR->config->item("base_url");
        	
        	if( substr($base_url,0,7) == 'http://') {
	        	$base_url = substr($base_url,7);
	        	$new_base_url .= "http://";
	        }
        	
	        if( substr($base_url,0,4) == 'www.' && $user_lang_abbr != $deft_abbr) {
	        	$base_url = substr($base_url,4);
	        	$new_base_url .= $user_lang_abbr . ".";
	        }
	        
	        $new_base_url .= $base_url;
	        
	        $RTR->config->set_item('base_url', $new_base_url);
        }
        
    }
}

/* Override CI current_url helper (from CI 1.7.0) */
function current_url()
{
    global $URI;
    $arr = explode('/',rtrim($URI->config->site_url(),'/'));
    if ($URI->config->item('language_abbr') == array_pop($arr))
        return implode('/',$arr).$URI->uri_string();
    return $URI->config->site_url($URI->uri_string());    
} 